/**
 * 影碟租赁系统
 * 
 */
const List = require('./listDefine') 
class Customer {
  constructor(name,sex) {
    this.name = name;
    this.sex = sex;
    this.movies = [];
  }
  append(movie){
    this.movies.push(movie)
  }
  // 归还
  giveBack(movie){
    const findAt = this.find(movie)
    if(findAt>-1){
      this.movies.splice(findAt,1)
    }
  }
  find(movie){
    for (let index = 0; index < this.movies.length; index++) {
      const element = this.movies[index];
      if(element === movie) return index
    }
    return -1
  }
  showMovies(){
    return this.movies.join(",")
  }
}
let movies = [
  "1_教父",
  "2_教父2",
  "3_肖申克的救赎",
  "4_低俗小说",
  "5_黄金三嫖客",
  "6_十二怒汉",
  "7_辛德勒的名单",
  "8_黑暗骑士",
  "9_指环王:王者归来",
  "10_搏击俱乐部",
  "11_星球大战:帝国反击战",
  "12_飞跃疯人院",
  "13_盗梦空间",
  "14_黑客帝国",
  "15_阿甘正传",
];
let filmList = new List();
let customerList = new List();
for (let index = 0; index < movies.length; index++) {
  const element = movies[index];
  filmList.append(element);
}

//显示列表
function displayList(list) {
  for (list.front(); list.curpos() <= list.length()-1; list.next()) {
    if (list.getElement() instanceof Customer) {
      //客户
      console.log(list.getElement()["name"] + "   " + list.getElement().showMovies());
    } else {
      // 电影清单
      console.log(list.getElement());
    }
    if(list.curpos() === list.length()-1) break
  }
}
//检出电影
function checkout(name, movie, filmList, customerList) {
  if (filmList.contains(movie)) {
    let c = checkCustomer(name,customerList)
    if(c) c.append(movie)
    else{
      c = new Customer(name,"男")
      c.append(movie)
      customerList.append(c)
    }
    filmList.remove(movie)
  }
}
// 归还电影 TODO 没有实现
function checkin(name,movie,filmList,customerList){
  filmList.append(movie)
  customerList.remove("chentao")
}
//检查用户是否已经借过电影
function checkCustomer(name,list){
  for (list.front(); list.curpos() <= list.length()-1; list.next()) {
    if(list.getElement().name === name) return list.getElement()
    if(list.curpos() === list.length()-1) break
  }
  return false
}
// 测试
// 检出前
// 检出后
checkout("chentao","14_黑客帝国",filmList,customerList)
checkout("chentao","15_阿甘正传",filmList,customerList)
console.log("----------------电影列表");
displayList(filmList)
console.log("----------------顾客列表");
// console.log(customerList.dataStore)
displayList(customerList)

